package bigdata_remote

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/common/utils"
)

type bigdataRepository struct{}

const (
	GetCustomerGroupCountUri = "/big-data/gfyx/customer-group/count"
)

func NewBigDataServiceRepo() BigDataServiceRepositoryIface {
	return &bigdataRepository{}
}

func (r *bigdataRepository) GetCustomerGroupCount(ctx context.Context, qrId int64) (int32, error) {
	client := utils.NewDataServiceClient()

	response := GetCustomerGroupCountResponse{}
	_, _, errs := client.Post(ctx, GetCustomerGroupCountUri, map[string]interface{}{"cg_id": qrId}, nil).EndStruct(&response)
	if len(errs) > 0 {
		return 0, errs[0]
	}

	if response.Success == false {
		return 0, errors.New(response.Message)
	}

	if len(response.Data.List) > 0 {
		return response.Data.List[0].Total, nil
	}

	return 0, nil
}
